/**
 * Copyright &copy; 2016 CNJSON All rights reserved.
 */
package com.hongsuan.pplive.modules.live.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cnjson.common.config.Global;
import com.cnjson.db.persistence.Page;
import com.cnjson.common.utils.StringUtils;
import com.cnjson.common.web.BaseController;
import com.hongsuan.pplive.modules.live.entity.LiveRedEnvelope;
import com.hongsuan.pplive.modules.live.service.LiveRedEnvelopeService;

/**
 * 单表生成Controller
 * @author cgli
 * @version 2016-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/live/liveRedEnvelope")
public class LiveRedEnvelopeController extends BaseController {

	@Autowired
	private LiveRedEnvelopeService liveRedEnvelopeService;

	@ModelAttribute
	public LiveRedEnvelope get(@RequestParam(required=false) String id) {
		LiveRedEnvelope entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = liveRedEnvelopeService.get(id);
		}
		if (entity == null){
			entity = new LiveRedEnvelope();
		}
		return entity;
	}

	@RequiresPermissions("live:liveRedEnvelope:view")
	@RequestMapping(value = {"list", ""})
	public String list(LiveRedEnvelope liveRedEnvelope, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<LiveRedEnvelope> page = liveRedEnvelopeService.findPage(new Page<LiveRedEnvelope>(request, response), liveRedEnvelope);
		model.addAttribute("page", page);
		return "modules/live/liveRedEnvelopeList";
	}

	@RequiresPermissions("live:liveRedEnvelope:view")
	@RequestMapping(value = "form")
	public String form(LiveRedEnvelope liveRedEnvelope, Model model) {
		model.addAttribute("liveRedEnvelope", liveRedEnvelope);
		return "modules/live/liveRedEnvelopeForm";
	}

	@RequiresPermissions("live:liveRedEnvelope:edit")
	@RequestMapping(value = "save")
	public String save(LiveRedEnvelope liveRedEnvelope, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, liveRedEnvelope)){
			return form(liveRedEnvelope, model);
		}
		liveRedEnvelopeService.save(liveRedEnvelope);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:"+Global.getAdminPath()+"/live/liveRedEnvelope/?repage";
	}

	@RequiresPermissions("live:liveRedEnvelope:edit")
	@RequestMapping(value = "delete")
	public String delete(LiveRedEnvelope liveRedEnvelope, RedirectAttributes redirectAttributes) {
		liveRedEnvelopeService.delete(liveRedEnvelope);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:"+Global.getAdminPath()+"/live/liveRedEnvelope/?repage";
	}

}